package com.foodev.maven.gwt;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.plexus.util.FileUtils;

/**
 * @phase clean
 * @goal  clean
 *
 * @author Martin Algesten
 */
public class CleanMojo extends AbstractGwtMojo {
  
  
  /**
   * The modules
   * 
   * @required
   * @parameter
   */
  private List<String> modules;

  
  public void execute() throws MojoExecutionException, MojoFailureException {

    try {
      
      deleteOutputDirs(modules);
      
    } catch ( IOException ioe ) {
      
      getLog().debug(ioe);
      
      throw new MojoFailureException("Failed to clean: "+ioe.getMessage());
      
    }
    
  }
  
  
  /**
   * Deletes the output directories.
   * @param modules the list of modules.
   * @throws IOException
   */
  private void deleteOutputDirs( List<String> modules ) throws IOException {
    
    for( String module : modules ) {
      
      File output = new File( warSourceDirectory, module );
     
      if ( output.exists() ) {
      
        getLog().info("Deleting directory "+output.getAbsolutePath());
  
        FileUtils.deleteDirectory(output);

      }
      
      File outputAux = new File( warSourceDirectory, module+"-aux" );
      
      if ( outputAux.exists() ) {
        
        getLog().info("Deleting directory "+outputAux.getAbsolutePath());
  
        FileUtils.deleteDirectory(outputAux);

      }
        
    }
    
  }


}
